﻿var currentUser;

var isDebug = false; //启用调试

// JavaScript Document
/*数据后台服务地址，请根据实际情况修改*/
var context_path = isDebug ? "http://192.168.0.5:91/" : "http://swimming.bingosoft.net/";

// 获取特定用户信息
var GetUserByLoginId = context_path + "dataservice.ashx?CommandName=UserService$GetUserByLoginId&loginId=";

var dataservice_path = context_path + "dataservice.ashx?CommandName=BisService$";

//根据ID获取用户
var GetUserAccountById = dataservice_path + "GetUserAccountById&userId=";
// 验证会员
var CheckLocalUser = dataservice_path + "ExistUser&username="; 
// 获取最近的活动列表
var GetActivityList = dataservice_path + "GetActivityList";
// 获取特定活动信息
var GetActivity = dataservice_path + "GetActivity&activityId=";
// 特定活动报名成员
var GetActivityUsers = dataservice_path + "GetActivityUsers&activityId=";
// 报名功能、撤销报名、(LoginId=&ActivityId=)
var Enter = dataservice_path + "Enter&";
var Cancel = dataservice_path + "Cancel&";
//签到
var SignIn = dataservice_path + "SignIn&activityUserId=";
//关闭活动
var Close = dataservice_path + "Close&activityId=";

// 获取{协会总额,总余额,经费总额,经费余额,会费总额,会费余额}
var GetAmount = dataservice_path + "GetAmount";

//获取每次活动消费总计及人数
var GetActivityFinanceList = dataservice_path + "GetActivityFinanceList";
var GetActivityFinanceUser = dataservice_path + 'GetActivityFinanceUser&activityId=';
var GetMonthFinance = dataservice_path + 'GetMonthFinance';

//得到协会全部成员
var GetUserList = dataservice_path + "GetUserList";

var GetUserFace = dataservice_path + 'GetFace&userId=';

BingoTouch.ExtFuc = {};

function ReqGet(url, sync, successCallback) {
    BingoTouch.Common.WebRequest(url, "GET", "", null, sync,
    function (response) {
        callback(response, successCallback);
    }, error);
}

function GetUserFace(userId, actFace) {
    BingoTouch.Common.WebRequest(GetUserFace + userId, "GET", "", null, sync,
    function (response) {
        
    }, error);
}

function error(code, desc) {
    BingoTouch.Common.alert("请求ServiceTicket失败!" + desc);
}

function callback(response, successCallback) {
    var retObj = BingoTouch.Common.string2json(response);
    if (retObj.returnCode == 200) {
        retObj = retObj.returnValue;
        if (retObj.Code == 200) {
            successCallback(retObj.Model, retObj.Message);
        } else if (retObj.Code == 888) {
            if (!!retObj.Message) {
                BingoTouch.Common.alert(retObj.Message);
            }
        }
    } else if (retObj.returnCode == 500) {
        BingoTouch.Common.alert('系统异常，请联系管理员！');
    }
}

function netCallBack(r) {
    //alert("回调成功。");
    //alert(r);
}
function progressCallBack() { alert('取消！'); }

function InitModule() {
    BingoTouch.Module.Form.init();
    BingoTouch.Module.List.init();
    BingoTouch.Module.Navbar.init();
    BingoTouch.Module.Collapse.init();

    $('#refresh').tap(function (e) {
        BingoTouch.WebView.refresh();
    });
}

function string2Date(str) {
    var arr = str.split('T');
    return arr[0] + ' ' + arr[1].substring(0, 5);
}


/*注销当前登录*/
function signOut() {
    if (BingoTouch.Common.confirm("确定要注销吗?")) {
        forceSignOut();
    }
}

function forceSignOut() {
    BingoTouch.Local.clearHashmap("loginHashMap");
    BingoTouch.WebView.load("login.html");
}

//每次显示前都会获取用户信息
BingoTouch.init = function () {

    var struser = '';
    if (isDebug) //调试时默认使用许鹏登陆
        struser = '{"Id":"43fe6476-cd7b-493b-8044-c7e3149d0876","Name":"许鹏","Type":"admin","LoginId":"xupeng","Password":"ICy5YqxZB1uWSwcVLSNLcA==","Email":"jadexupeng@gmail.com","Face":"Face/43fe6476-cd7b-493b-8044-c7e3149d0876.jpg","MobilePhone":"15013006247","Im":"9808175","Telephone":"15013006247","Sex":"U","Birthday":"1985-06-06T00:00:00.00+08:00","Status":"enabled","OrgId":"bfab3e63-6a5d-46ee-b284-ae77cae2074e","Rank":2,"Consume":46.0,"Balance":53.5,"AttendTimes":2,"LostTimes":0,"ReloadCount":0,"ReloadAmount":0.0,"SnatchCard":20,"ProtectCard":0,"CreatedBy":"43FE6476-CD7B-493B-8044-C7E3149D0876","CreatedDate":"2013-04-18T23:55:47.64+08:00","LastUpdatedBy":"43FE6476-CD7B-493B-8044-C7E3149D0876","LastUpdatedDate":"2013-04-22T20:34:11.86+08:00","Position":"会长"}'
    else
        struser = BingoTouch.Local.readHashmap("loginHashMap", "currentUser");

    currentUser = BingoTouch.Common.string2json(struser);
}

function checkLogin() {
    if (!!!currentUser)
        BingoTouch.WebView.load("login.html", 'none');
}

/**    
* 对Date的扩展，将 Date 转化为指定格式的String    
* 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q) 可以用 1-2 个占位符    
* 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)    
* eg:    
* (new Date()).pattern("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423    
* (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04    
* (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04    
* (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04    
* (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18    
*/
Date.prototype.pattern = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份      
        "d+": this.getDate(), //日      
        "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时      
        "H+": this.getHours(), //小时      
        "m+": this.getMinutes(), //分      
        "s+": this.getSeconds(), //秒      
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度      
        "S": this.getMilliseconds() //毫秒      
    };
    var week = {
        "0": "/u65e5",
        "1": "/u4e00",
        "2": "/u4e8c",
        "3": "/u4e09",
        "4": "/u56db",
        "5": "/u4e94",
        "6": "/u516d"
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    if (/(E+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[this.getDay() + ""]);
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
}
